package com.zhiyou100.video.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhiyou100.video.model.Course;
import com.zhiyou100.video.model.Speaker;
import com.zhiyou100.video.model.Subject;
import com.zhiyou100.video.model.Video;
import com.zhiyou100.video.service.CourseService;
import com.zhiyou100.video.service.SpeakerService;
import com.zhiyou100.video.service.SubjectService;
import com.zhiyou100.video.service.VideoService;

@Controller
@RequestMapping("/front")
public class FrontController {

	@Autowired
	private CourseService cs;

	@Autowired
	private VideoService vs;

	@Autowired
	private SubjectService sts;

	@Autowired
	private SpeakerService srs;

	@RequestMapping("/course/index")
	public String course(int subjectId,Model model) {
		List<Course> courses = cs.selectBySubjectId(subjectId);
		for (Course course : courses) {
			Subject subject = course.getSubject();
			model.addAttribute("subject", subject);
			List<Video> videos = course.getVideos();
			for (Video video : videos) {
				int h = Integer.parseInt(video.getVideo_length())/3600;
				int m = Integer.parseInt(video.getVideo_length())%3600/60;
				int s = Integer.parseInt(video.getVideo_length())%3600%60;
				String hh = h+"";
				if(hh.length()<2) {
					hh="0"+String.valueOf(h);
				}
				String mm = m+"";
				if(mm.length()<2) {
					mm="0"+String.valueOf(m);
				}
				String ss = s+"";
				if(ss.length()<2) {
					ss="0"+String.valueOf(s);
				}

				String x = hh+" : "+mm+" : "+ss;

				video.setVideo_length(x);
				System.out.println("????????????????????????????????"+video.getVideo_length());
			}
		}
		model.addAttribute("courses", courses);
		return "front/course/index";
	}


	@RequestMapping("/video/index")
	public String video(Integer subject_id,int videoId,Model model) {
		System.out.println("*********************"+subject_id);
		System.out.println("@@@@@@@@@@@@@@@@@@@@@"+videoId);

		Subject subject = sts.selectById(subject_id);
		Video video = vs.selectVideoById(videoId);
		System.out.println("########################"+video.getVideo_play_times());
		video.setVideo_play_times(video.getVideo_play_times()+1);
		System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&"+video.getVideo_play_times()+1);
		int i = vs.updateVideo(video);
		System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!"+i);
		model.addAttribute("subject", subject);
		model.addAttribute("video", video);
		return "front/video/index";
	}


	@RequestMapping("/video/videoData.do")
	public String content(int videoId,Model model,Map<String, Object> map) {
		System.out.println("++++++++++++++++++"+videoId);

		Video video = vs.selectVideoById(videoId);
		int course_id = video.getCourse_id();
		Course course = cs.selectById(course_id);
		int speaker_id = video.getSpeaker_id();
		Speaker speaker = srs.selectById(speaker_id);
		List<Video> videos = vs.selectVideo(map);

		model.addAttribute("video", video);

		model.addAttribute("course", course);
		model.addAttribute("speaker", speaker);
		model.addAttribute("videoList", videos);
		return "front/video/content";
	}
}
